//
//  HJSettingController.m
//  KaiChat
//
//  Created by lx on 2024/1/16.
//

#import "HJSettingController.h"
#import "HJIconTitleCell.h"
#import "HJIconTitleRedPointCell.h"
#import "HJUIAlertController.h"
#import "HJMinePresent.h"
#import "HJUserModel.h"

@interface HJSettingController ()

@property (nonatomic, strong) NSArray *datas;


@end

@implementation HJSettingController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.navigationBar.title = @"Settings";
    self.navigationBar.lineView.hidden = YES;
    
    [self.tableView registerClass:HJIconTitleCell.class forCellReuseIdentifier:@"HJIconTextCell"];
    [self.tableView registerClass:HJIconTitleRedPointCell.class forCellReuseIdentifier:@"HJIconTitleRedPointCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.rowHeight = cellHeight;
    self.tableView.backgroundColor = UIColor.whiteColor;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.offset(KNavBarHight);
        make.bottom.offset(0);
    }];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleDidClickBindingGoogleOrAppleNotification) name:didClickBindingGoogleOrAppleNotification object:nil];
    
}

- (void)handleDidClickBindingGoogleOrAppleNotification {
    self -> _datas = nil;
    [self.tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HJCellModel *model = self.datas[indexPath.row];
    HJIconTitleCell *cell = nil;
    if ([model.identifier isEqualToString:@"security"]) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"HJIconTitleRedPointCell" forIndexPath:indexPath];
    } else {
        cell = [tableView dequeueReusableCellWithIdentifier:@"HJIconTextCell" forIndexPath:indexPath];
    }
    cell.model = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    HJCellModel *model = self.datas[indexPath.row];
    
    if ([model.identifier isEqualToString:@"logout"]) {
        HJUIAlertController *alert = [HJUIAlertController alertControllerWithTitle:nil message:@"Confirm to quit the account?" preferredStyle:HJUIAlertControllerStyleActionSheet];
        [alert addAction:[HJUIAlertAction actionWithTitle:@"OK" style:HJUIAlertActionStyleDestructive handler:^(HJUIAlertAction * _Nonnull action) {
            [HJMinePresent logoutComplete:nil];
            [[NSNotificationCenter defaultCenter] postNotificationName:logoutNotification object:nil];
        }]];
        [alert addAction:[HJUIAlertAction actionWithTitle:@"Cancel" style:HJUIAlertActionStyleCancel handler:^(HJUIAlertAction * _Nonnull action) {
            
        }]];
        [alert showInViewController:self];
        return;
    }
    
    NSString *controllerName = model.controllerName;
    if (isEmptyString(controllerName)) {
        return;
    }
    
    [self.navigationController pushViewControllerWithName:controllerName];
}

- (NSArray *)datas {
    if (!_datas) {
        _datas = @[
            [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                model.iconName = @"setting_security";
                model.identifier = @"security";
                model.text = @"Account Security";
                model.controllerName = @"HJAccountSettingController";
                model.showArrow = YES;
                
                if (isHiddenAppleServe()) {
                    if (!isEmptyString(HJUserInfo.model.googleEmail)) {
                        model.showRedPoint = NO;
                    } else {
                        model.showRedPoint = !HJUserInfo.model.bindGoogleRead;
                    }
                } else {
                    if (!isEmptyString(HJUserInfo.model.googleEmail) && !isEmptyString(HJUserInfo.model.appleUserId)) {
                        model.showRedPoint = NO;
                    } else {
                        model.showRedPoint = !HJUserInfo.model.bindGoogleRead || !HJUserInfo.model.bindAppleRead;
                    }
                }
            }],
            [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                model.iconName = @"setting_notice";
                model.identifier = @"notice";
                model.text = @"Notifcation Sound";
                model.controllerName = @"HJNotificationSettingController";
                model.showArrow = YES;
            }],
            [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                model.iconName = @"setting_private";
                model.text = @"Privacy Security";
                model.identifier = @"Security";
                model.controllerName = @"HJPrivacyController";
                model.showArrow = YES;
            }],
            [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                model.iconName = @"setting_logout";
                model.identifier = @"logout";
                model.text = @"Log out";
                model.showArrow = NO;
            }],
        ];
    }
    return _datas;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    TJLog(@"%s", __func__);
}

@end
